package com.android.baselibrary.widget.MRecylcer.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.baselibrary.recycleradapter.BaseViewHolder;

import java.util.List;

public abstract class YQRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    private int listitem;
    private List<T> mDatas;
    private Context mContext;


    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public YQRecyclerAdapter(Context context, int listitem, List<T> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
        this.listitem = listitem;
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(mContext).inflate(listitem, parent, false);
        return new MViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
//        item =   mDatas.get(position);
        convert(holder, position, mDatas.get(position));
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    int position = holder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //  int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView, position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }
    }

    public abstract void convert(BaseViewHolder holder, final int position, T item);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void addData(List<T> newData) {
        this.mDatas.addAll(newData);
        this.notifyDataSetChanged();
    }

    public void setData(List<T> newData) {
        this.mDatas.clear();
        this.mDatas.addAll(newData);
        this.notifyDataSetChanged();
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public void clearData() {
        mDatas.clear();
    }

    public void removeItem(int p) {
        mDatas.remove(p);
        this.notifyDataSetChanged();
    }


    class MViewHolder extends BaseViewHolder {

        public MViewHolder(View itemView) {
            super(itemView);
        }
    }
}
